<template>
<div>
    <el-card class="box-card">
            <!-- 搜索表单-->
        <el-form :inline="true">
            <el-form-item>
                <el-button
                icon="el-icon-plus" 
                size="mini" 
                type="primary"
                @click="dialogTitle = '添加商品详细分类'
                ,dialogVisible = true
                ,form = {}
                ,img = false">
                    添加商品详细分类
                </el-button>
            </el-form-item>
        </el-form>
        <el-table
        :data="tableData"
        stripe
        style="width: 100%">
            <el-table-column
            prop="id"
            label="id"
            align="center">
            </el-table-column>
            <el-table-column
            prop="litimgUrl"
            label="缩略图"
            align="center">
                <template slot-scope="scope">
                    <img v-if="scope.row.litimgUrl" :src="scope.row.litimgUrl" class="url">
                </template>
            </el-table-column>
            <el-table-column
            prop="typeValue"
            label="分类"
            align="center">
            </el-table-column>
            <el-table-column
            prop="name"
            label="详细分类"
            align="center">
            </el-table-column>
            <el-table-column
            width="100"
            fixed="right"
            align="center"
            label="操作">
                <template slot-scope="scope">
                    <i class="el-icon-edit" style="font-size: 12px;color: #409EFF;cursor:pointer;" @click="updateCommodityType(scope.row.id)">编辑</i>
                    <i class="el-icon-delete" style="font-size: 12px;color: #F56C6C;cursor:pointer;" @click="remove(scope.row.id)">删除</i>
                </template>
            </el-table-column>
        </el-table>
        <!--分页栏-->
            <el-pagination
            background
            align="center"
            @size-change="handleSizeChange($event,111)"
            @current-change="data()"
            :current-page.sync="currentPage"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
            </el-pagination>
    </el-card>
    <!--添加框/编辑框-->
    <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose">
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="缩略图">
          <el-upload
          class="commodityType-uploader"
          :action="uploadUrl"
          :headers="headers"
          :show-file-list="false"
          :on-success="handleCommodityTypeSuccess"
          :before-upload="beforeCommodityTypeUpload">
            <img v-if="img" :src="form.litimgUrl" class="commodityType">
            <i v-else class="el-icon-plus commodityType-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="所属分类">
            <el-select
            v-model="form.type"
            style="width:100%">
                <el-option
                v-for="item in facilityTypes"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="详细分类名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submmit()">确 定</el-button>
      </span>
    </el-dialog>
</div>
</template>
<script>
import { list,add,del,update,commodityType } from '@/api/app_pages/commodityType'
import { getDicts } from '@/api/system/dict/data'
import { getToken } from '@/utils/auth'
export default {
    components:{
    },
    data(){
        return {
            tableData: [],
            facilityTypes: [],
            dialogTitle: '',
            form:{},
            dialogVisible: false,
            img: false,
            uploadUrl: process.env.VUE_APP_BASE_API + "/common/upload",
            headers: {
                Authorization: 'Bearer ' + getToken()
            }
        }
    },
    created(){
        this.data()
        getDicts('g_facility_type').then(res => {
            let data = []
            res.data.forEach(item => {
                data.push({label:item.dictLabel,value:item.dictValue})
            });
            this.facilityTypes = data
        })
    },
    methods:{
        data(){
            list(this.size,this.currentPage).then(res => {
                this.tableData = res.rows
                this.total = parseInt(res.total)
            })
        },
        handleSizeChange(val,e) {
            this.size = val;
            this.data()
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
            .then(_ => {
                done();
            })
            .catch(_ => {});
        },
        beforeCommodityTypeUpload(file){
            const isImg = file.type.indexOf("image/")==0;
            if (!isImg) {
            this.$message.error('图片格式不正确!');
            }
            return isImg;
        },
        handleCommodityTypeSuccess(res, file){
            this.form.litimgUrl = res.fileName
            this.img = true
        },
        updateCommodityType(id){
            this.dialogTitle = '编辑商品详细分类'
            this.dialogVisible = true
            this.form.id = parseInt(id);  
            commodityType(this.form.id).then(res => {
                this.form = res.data 
                this.img = true
            })
        },
        submmit(){
            if(this.dialogTitle == '编辑商品详细分类'){
                new Promise((resolve,reject) =>{
                update(this.form).then(res => {
                    this.dialogVisible = false
                    resolve()
                })
                }).then(res => {
                    this.data()
                })
            
            }else{
                new Promise((resolve,reject) =>{
                    add(this.form).then(res => {
                        this.dialogVisible = false
                        resolve()
                    })  
                }).then(res => {
                    this.data()
                })
            }
        },
        remove(id){
            new Promise((resolve,reject) =>{
                del(id).then(res => {
                    resolve()
                })  
            }).then(res => {
                this.$message('删除成功')
                this.data()
            })   
        }
    }
}
</script>
<style>
    .text {
    font-size: 14px;
    }
    .item {
    margin-bottom: 18px;
    }
    .clearfix:before,
    .clearfix:after {
    display: table;
    content: "";
    }
    .clearfix:after {
    clear: both
    }
    .box-card {
    margin: 0 auto;
    }

    .commodityType-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .commodityType-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .commodityType-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .commodityType {
    width: 178px;
    height: 178px;
    display: block;
  }
  .url {
        width: 50px;
        height: 50px;
        display: block;
        margin: 0 auto;
    }
</style>